﻿using DbControl;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;

namespace CentralMonitoring
{
    /// <summary>
    /// 参数配置窗体
    /// </summary>
    public partial class ParametsEdit : Form
    {    
        public int m_Id = -1;       
        public OprType m_type = OprType.Add;
        public string m_beforName = "";
        private string m_beforCode = "";
        private parameters m_devices;
        public ParametsEdit()
        {
            InitializeComponent();
        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxName.Text) || textBoxName.Text.Length < 1)
            {
                MessageBox.Show("名称为必填项！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrEmpty(textBoxName.Text) || textBoxName.Text.Length < 1)
            {
                MessageBox.Show("编码为识别码与终端绑定,为必填项！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }           
            if (m_type == OprType.Add)
            {
                parameters pa = new  parameters();
                if (!string.IsNullOrEmpty(textBoxCode.Text))
                    pa.parameter_code = textBoxCode.Text;
                pa.parameter_name = textBoxName.Text;
                if (!string.IsNullOrEmpty(textBoxUnit.Text))
                    pa.parameter_unit = textBoxUnit.Text;
                switch (comboBoxDatatype.SelectedIndex)
                {
                    case 0:pa.parameter_datatype = "int";break;
                    case 1:pa.parameter_datatype="float";break;
                }
                if (!string.IsNullOrEmpty(textBoxPoint.Text))
                    pa.parameter_point = textBoxPoint.Text;
                if (!string.IsNullOrEmpty(textBoxAttribute.Text))
                    pa.parameter_attribute = textBoxAttribute.Text;
                if (!string.IsNullOrEmpty(textBoxType.Text))
                    pa.parameter_type=textBoxType.Text;
                pa.flag= comboBoxFlag.SelectedIndex;               
                if (OprParameter.IsExistParameterName(textBoxName.Text))
                {
                    MessageBox.Show("名称已存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;

                }
                if (OprParameter.IsExistParameterCode(textBoxCode.Text))
                {
                    MessageBox.Show("识别码已存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                try
                {
                    OprParameter.Insert(pa);
                    this.DialogResult = DialogResult.OK;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("操作失败！" + ex.Message.ToString());
                } 
            }
            else
            {
                if (OprParameter.IsExistParameterName(textBoxName.Text) && m_beforName != textBoxName.Text)
                {
                    MessageBox.Show("名称已存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (!string.IsNullOrEmpty(m_beforCode))
                {
                    if (OprParameter.IsExistParameterCode(textBoxCode.Text) && m_beforCode != textBoxCode.Text)
                    {
                        MessageBox.Show("编码已存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
                try
                {
                    if (!string.IsNullOrEmpty(textBoxCode.Text))
                        m_devices.parameter_code = textBoxCode.Text;
                    m_devices.parameter_name = textBoxName.Text;
                    if (!string.IsNullOrEmpty(textBoxUnit.Text))
                        m_devices.parameter_unit = textBoxUnit.Text;
                    switch (comboBoxDatatype.SelectedIndex)
                    {
                        case 0: m_devices.parameter_datatype = "int"; break;
                        case 1: m_devices.parameter_datatype = "float"; break;
                    }
                    if (!string.IsNullOrEmpty(textBoxPoint.Text))
                        m_devices.parameter_point = textBoxPoint.Text;
                    if (!string.IsNullOrEmpty(textBoxAttribute.Text))
                        m_devices.parameter_attribute = textBoxAttribute.Text;
                    if (!string.IsNullOrEmpty(textBoxType.Text))
                        m_devices.parameter_type = textBoxType.Text;
                    m_devices.flag = comboBoxFlag.SelectedIndex;
                    OprParameter.Update(m_devices);
                    this.DialogResult = DialogResult.OK;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("操作失败！" + ex.ToString());
                }
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ParametsEdit_Load(object sender, EventArgs e)
        {
            if (m_Id > 0)
            {
                m_devices = OprParameter.GetParameterById(m_Id);
                if (m_devices != null)
                {
                    
                    textBoxCode.Text = m_devices.parameter_code;
                    textBoxName.Text = m_devices.parameter_name;                                     
                    if(!string.IsNullOrEmpty(m_devices.parameter_unit))
                    {
                        textBoxUnit.Text = m_devices.parameter_unit;
                    }                        
                    if(!string.IsNullOrEmpty(m_devices.parameter_datatype))
                    {
                       if(m_devices.parameter_datatype.Contains("int"))
                        {
                            comboBoxDatatype.SelectedIndex = 0;
                        }
                        else
                        {
                            comboBoxDatatype.SelectedIndex = 1;
                        }                 
                    }
                    if(!string.IsNullOrEmpty(m_devices.parameter_point))
                    {
                        textBoxPoint.Text = m_devices.parameter_point;
                    }
                    if(!string.IsNullOrEmpty(m_devices.parameter_attribute))
                    {
                        textBoxAttribute.Text = m_devices.parameter_attribute;
                    }
                    if (!string.IsNullOrEmpty(m_devices.parameter_type))
                    {
                        textBoxType.Text = m_devices.parameter_type;
                    }
                    if (m_devices.flag.HasValue)
                    {
                        comboBoxFlag.SelectedIndex = m_devices.flag.Value;
                    }                   
                }
            }
        }

       
    }
}
